package biostienda.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import biostienda.dao.UsuarioDAO;
//import biostienda.excepcion.DAOClsException;
import biostienda.excepcion.DAOExcepcion;
import biostienda.negocio.GestionUsuarios;
import biostienda.util.ConexionBD;

/**
 * Servlet implementation class RegistroUsuarioServlet
 */
@WebServlet("/RegistroUsuarioServlet")
public class RegistroUsuarioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistroUsuarioServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		HttpSession respuesta = request.getSession(true);
		
		
		String b = request.getParameter("apellido");
		String c = request.getParameter("nombre");
		String d = request.getParameter("sexo");
		String e = request.getParameter("dni");
		String f = request.getParameter("correo");
		String g = request.getParameter("celular");
		String h = request.getParameter("nacimiento");
		String i = request.getParameter("usuario");
		String j = request.getParameter("contrasena");
		String k = request.getParameter("tipo");
		
		int dni= Integer.parseInt(e);
		int cel= Integer.parseInt(g);
		int tipo= Integer.parseInt(k);
		
		SimpleDateFormat parse = new SimpleDateFormat("dd-MM-yyyy");

		String strfecha = h;
		java.util.Date datefecha = null;
		try {
			datefecha = parse.parse(strfecha);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		java.sql.Date fecha = new java.sql.Date(datefecha.getTime());
		
		GestionUsuarios negocio = new GestionUsuarios();
		
		try {
            negocio.insertar( b, c, d, dni, f, cel, fecha, i, j,tipo);
            response.sendRedirect(request.getContextPath()  + "/registrovalido.jsp");
    } catch (DAOExcepcion q) {

		RequestDispatcher rd = request.getRequestDispatcher("registrousuario.jsp");
		rd.forward(request, response);
}
		
		
		//RequestDispatcher rd = request.getRequestDispatcher("registrousuario.jsp");
		//rd.forward(request, response);
		
		
	
			}
}
